<?php

namespace App\Http\Middleware;

use App\Models\AdminLog;
use Closure;
use Illuminate\Support\Facades\DB;

class ActionLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $admin_id = $request->cookie('admin_id');

        if(!$admin_id){
            return redirect('/admin/login/index');
        }
        AdminLog::insert([
            'admin_id' => $admin_id,
            'path' => $request->path(),
            'method' => $request->method(),
            'data' => json_encode($request->all())
        ]);

        return $response;
    }
}
